<?php
namespace app\index\controller;

use think\Cache;

class Ajax extends \think\Controller
{


    public function join()//用户加入
    {
        $user_list = cache('user_list') ? cache('user_list') : array();
        if (!in_array(['uid' => input('username')], $user_list)) {
            $res = array_push($user_list, ['uid' => input('username')]);//插入
            if ($res) {
                $return = 1;
                cache('user_list', $user_list);
            } else {
                $return = 0;
            }
        } else {
            $return = 1;
        }
        return $return;
    }

    public function out()//用户退出
    {
        $user_list = cache('user_list');
        if (in_array(['uid' => input('username')], $user_list)) {
            unset($user_list[array_search(['uid' => input('username')], $user_list)]);//删除
            cache('user_list', $user_list);
        }
        return $this->user_list();
    }

    public function user_list()//用户列表数据
    {
        $data = cache('user_list');
        $this->assign('data', $data);
        $return['user_list'] = $this->fetch('ajax/user_list');
        $return['count'] = count($data);
        return $return;
    }

    public function send()//用户群发发消息
    {
        $input = input();
        if ($input['msg']) {
            $input['time'] = time();
            $user_mes = cache("user_mes") ? cache("user_mes") : array();
            array_push($user_mes, $input);//插入
            $res = cache('user_mes', $user_mes);
            if ($res) {
                $return['mes'] = "设置成功";
                $return['status'] = 200;
                $return['data'] = $this->send_str($input);
            } else {
                $return['mes'] = "系统繁忙";
                $return['status'] = 404;
            }
        } else {
            $return['mes'] = "请输入消息内容";
            $return['status'] = 404;
        }
        return $return;
    }

    public function send_one()//用户私聊信息
    {
        $input = input('');
        if ($input['msg']) {
            $input['time'] = time();
            $code = $input['send_uid'] < $input['receive_uid'] ? $input['send_uid'] . $input['receive_uid'] : $input['receive_uid'] . $input['send_uid'];
            $mes_one = cache('mes_one_' . $code) ? cache('mes_one_' . $code) : array();
            array_push($mes_one, $input);//插入
            $res = cache('mes_one_' . $code, $mes_one);
            if ($res) {
                $return['mes'] = "设置成功";
                $return['status'] = 200;
                $return['data'] = $this->send_str_one($input);
            } else {
                $return['mes'] = "系统繁忙";
                $return['status'] = 404;
            }
        } else {
            $return['mes'] = "请输入消息内容";
            $return['status'] = 404;
        }
        return $return;
    }


    public function send_str($arr)//用户群发发消息字符串
    {
        return "<div><h3 style='margin-left: 30%;'>" . date("m-d H:i:s", $arr['time']) . "</h3><span>" . $arr['uid'] . "</span>::<span>" . $arr['msg'] . "</span></div>";
    }

    public function send_str_one($arr)//用户私聊发消息字符串
    {
        return "<div><h3 style='margin-left: 30%;'>" . date("m-d H:i:s", $arr['time']) . "</h3><span>" . $arr['send_uid'] . "</span>::<span>" . $arr['msg'] . "</span></div>";
    }
}
